package com.devp.config;

import com.devp.interceptor.ParamInterceptor;
import com.devp.wsdl.provider.DomInterfaceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * create by yongli on 2021-05-19 20:35
 */
@Configuration
public class CxfConfig {

    @Autowired
    ParamInterceptor paramInterceptor;
    @Autowired
    private Bus bus;


    /**
     * 作用：改变服务名的前缀名
     * 此方法被注释后：wsdl访问（默认）地址为http://127.0.0.1:8080/services/user?wsdl
     * 去掉注释后：wsdl访问地址为：http://127.0.0.1:8080/mySoap/user?wsdl
     *
     * @return
     */
    @SuppressWarnings("all")
    @Bean(name = "dispatchServletwsdl")
    public ServletRegistrationBean dispatchServletwsdl() {
        return new ServletRegistrationBean(new CXFServlet(), "/DomInterface/*");
    }

    /**
     * 这个bus是干啥的
     *
     * @return
     */

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /***
     * 这个就是要发布的接口
     * @return
     */
    @Bean
    public DomInterfaceImpl domInterface() {
        return new DomInterfaceImpl();
    }


    /**
     * JAX-WS：EndpointImpl
     * 站点服务：终端路径
     *
     * @return
     */
    @Bean(name = "endpointwsdl")
    public Endpoint endpointwsdl() {
        EndpointImpl endpointImpl = new EndpointImpl(bus, domInterface());
        endpointImpl.getInInterceptors().add(paramInterceptor);
        endpointImpl.publish("/interface");
        return endpointImpl;
    }

}
